package com.xiao.androidcompose01.net.plugin

import android.app.Application
import com.xiao.androidcompose01.file.AccountFileUtil
import io.ktor.client.plugins.api.createClientPlugin
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.http.ContentType
import org.koin.compose.getKoin
import org.koin.compose.koinInject
import org.koin.core.context.GlobalContext

val AuthenticationPlugin = createClientPlugin("AuthenticationPlugin") {
   onRequest { request: HttpRequestBuilder, d:Any->
      val application = GlobalContext.get().inject<Application>().value
      
      val utils = AccountFileUtil(application)
      val jsessionid = utils.loadAccount()?.jsessionid
      if (!jsessionid.isNullOrBlank()){
         request.headers
            .append("Cookie", "JSESSIONID=${jsessionid}")
      }
      
   }
   
   onResponse { response->
   
   }
}